package com.adolph.netty.bio;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 〈功能概述〉<br>
 * BIO服务端
 * @author: wangxiangjun
 * @date: 2020-09-30 23:40
 */

public class BIOServer {

    public static void main(String[] args) throws IOException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        //服务端端口
        ServerSocket serverSocket = new ServerSocket(6666);
        System.out.println("服务器启动");
        while (true){
            //监听客户端
            Socket accept = serverSocket.accept();
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    handler(accept);
                }
            });
        }

    }


    public static void handler(Socket socket){
        try {
            byte[] bytes = new byte[1024];
            InputStream stream = socket.getInputStream();
            while (true){
                int len = stream.read(bytes);
                if(len!=-1){
                    System.out.println("读取数据===="+new String(bytes,0,len));
                }
            }
        }catch (Exception e){
            try {
                socket.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}
